<template>
    <div class="cart-container">
        <h2 class="cart-title">购物车</h2>
        <div class="cart-list">
            <div v-if="cartItems.length > 0">
                <div 
                    v-for="item in cartItems" 
                    :key="item.id" 
                    class="cart-item"
                >
                    <div class="cart-image">
                        <img :src="item.frame.image" alt="车架">
                    </div>
                    <div class="cart-info">
                        <h3 class="cart-name">{{ item.frame.name }}</h3>
                        <p class="cart-price">¥{{ item.frame.price.toLocaleString() }}</p>
                        <button class="remove-btn" @click="removeFromCart(item.id)">移除</button>
                    </div>
                </div>
            </div>
            <div v-else class="empty-cart">
                <p>购物车是空的</p>
                <button @click="goToParts">去选购零件</button>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { getUserCartList, removeFromCart } from '@/api/index.js'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const router = useRouter()
const cartItems = ref([])

// 获取购物车数据
const loadCartItems = async () => {
    try {
        const res = await getUserCartList()
        cartItems.value = res.data
    } catch (error) {
        ElMessage.error('加载购物车失败')
    }
}

// 移除购物车中的商品
const removeFromCart = async (id) => {
    try {
        await removeFromCart(id)
        ElMessage.success('移除成功')
        loadCartItems() // 重新加载购物车
    } catch (error) {
        ElMessage.error('移除失败')
    }
}

// 跳转到零件选择页面
const goToParts = () => {
    router.push('/part')
}

onMounted(() => {
    loadCartItems()
})
</script>

<style lang="css" scoped>
.cart-container {
    padding-top: calc(56px + env(safe-area-inset-top));
    min-height: 100vh;
    background: #f5f7fa;
    padding-bottom: 80px;
}

.cart-title {
    font-size: 24px;
    font-weight: 600;
    margin: 16px;
}

.cart-list {
    padding: 16px;
    background: white;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.cart-item {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
}

.cart-image {
    width: 80px;
    height: 80px;
    overflow: hidden;
    border-radius: 8px;
}

.cart-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.cart-info {
    flex: 1;
    padding-left: 12px;
}

.cart-name {
    font-size: 16px;
    font-weight: 500;
}

.cart-price {
    font-size: 14px;
    color: #f56c6c;
}

.remove-btn {
    background: #ff4d4f;
    color: white;
    border: none;
    padding: 6px 12px;
    border-radius: 8px;
    cursor: pointer;
}

.empty-cart {
    text-align: center;
    padding: 40px;
}

.empty-cart button {
    background: #409eff;
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 8px;
    cursor: pointer;
}
</style> 